package dk.mirasola.systemtraining.biddingsession.client.ui;

import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import dk.mirasola.systemtraining.biddingsession.shared.transfer.SessionBoardTO;

import java.util.ArrayList;
import java.util.List;

public class BoardsPanel extends Composite {
    private List<SessionBoardTO> sessionBoards;
    private Grid grid;
    private String selectedBoardKey;
    private ChangeHandler changeHandler;

    public BoardsPanel() {
        grid = new Grid();
        initWidget(grid);

        grid.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                HTMLTable.Cell cell = grid.getCellForEvent(event);
                int clickedBoardNumber = (cell.getRowIndex() * 10) + cell.getCellIndex() + 1;
                for (SessionBoardTO boardTO : BoardsPanel.this.sessionBoards) {
                    if (boardTO.getBoardNumber() == clickedBoardNumber && !boardTO.getBoardKey().equals(selectedBoardKey)) {
                        setSelectedBoardKey(boardTO.getBoardKey());
                        if (changeHandler != null) {
                            changeHandler.onChange(null);
                        }
                        break;
                    }
                }
            }
        });

        grid.addStyleName("boardsPanel");
    }

    public void setSessionBoards(List<SessionBoardTO> sessionBoards) {
        this.sessionBoards = new ArrayList<SessionBoardTO>(sessionBoards);

        int rows = sessionBoards.size() / 10;
        grid.resize(rows, 10);
        grid.setCellSpacing(2);

        int row = 0;
        int col = 0;
        for (SessionBoardTO  board : sessionBoards) {
            grid.setText(row, col, String.valueOf(board.getBoardNumber()));
            HTMLTable.CellFormatter cellFormatter = grid.getCellFormatter();
            if (board.getState() == 0) {
                cellFormatter.addStyleName(row, col, "finish");
            } else if (board.getState() == 1) {
                cellFormatter.addStyleName(row, col, "tobid");
            } else {
                cellFormatter.addStyleName(row, col, "alreadybid");
            }
            if (col < 9) {
                col++;
            } else {
                col = 0;
                row++;
            }
        }
    }

    public String getSelectedBoardKey() {
        return selectedBoardKey;
    }

    public void setSelectedBoardKey(String boardKey) {
        for (SessionBoardTO board : sessionBoards) {
            int row = getRow(board);
            int col = getCol(board);
            if (board.getBoardKey().equals(boardKey)) {
                this.selectedBoardKey = boardKey;
                grid.getCellFormatter().addStyleName(row, col, "selected");
            } else {
                grid.getCellFormatter().removeStyleName(row, col, "selected");
            }
        }
    }

    public void setChangeHandler(ChangeHandler changeHandler) {
        this.changeHandler = changeHandler;
    }

    private int getCol(SessionBoardTO board) {
        return (board.getBoardNumber() - 1) % 10;
    }

    private int getRow(SessionBoardTO board) {
        return ((board.getBoardNumber() - 1) / 10);
    }


    public void markSelectedToBidInSession() {
        for (SessionBoardTO board : sessionBoards) {
            if (board.getBoardKey().equals(selectedBoardKey)) {
                int row = getRow(board);
                int col = getCol(board);
                grid.getCellFormatter().removeStyleName(row, col,"alreadybid");
                grid.getCellFormatter().removeStyleName(row, col, "finish");
                grid.getCellFormatter().addStyleName(row, col, "tobid");
            }
        }
    }

    public void markSelectedBidInSession() {
        for (SessionBoardTO board : sessionBoards) {
            if (board.getBoardKey().equals(selectedBoardKey)) {
                board.setState(1);
                int row = getRow(board);
                int col = getCol(board);
                grid.getCellFormatter().removeStyleName(row, col, "tobid");
                grid.getCellFormatter().addStyleName(row, col, "alreadybid");
            }
        }
    }

    public void markSelectedFinish() {
        for (SessionBoardTO board : sessionBoards) {
            if (board.getBoardKey().equals(selectedBoardKey)) {
                board.setState(2);
                int row = getRow(board);
                int col = getCol(board);
                grid.getCellFormatter().removeStyleName(row, col, "tobid");
                grid.getCellFormatter().addStyleName(row, col, "finish");
            }
        }
    }
}
